package nimble.pay.trade.server.system;

import nimble.pay.trade.server.framework.TradeChannelManager;
import nimble.pay.trade.channel.AbstractTradeChannelProvider;
import nimble.pay.trade.channel.TradeChannelProvider;
import org.reflections.Reflections;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.Set;

/** 支付渠道管理器
 * @author max woods
 * @date 2018/8/6
 */

@Component
public class DefaultTradeChannelManager implements TradeChannelManager {

  private static final Logger logger= LoggerFactory.getLogger(DefaultTradeChannelManager.class);

  public DefaultTradeChannelManager()
  {
    Reflections reflections=new Reflections("nimble.pay.trade.channel");
    Set<Class<? extends AbstractTradeChannelProvider>> tradeChannelProviderClasses=reflections.getSubTypesOf(AbstractTradeChannelProvider.class);
    logger.info("========支付渠道数量:{}==========",tradeChannelProviderClasses.size());
    // 支付渠道实例化
    for(Class<? extends TradeChannelProvider> tradeChannelProviderClass:tradeChannelProviderClasses)
    {
      try {
        TradeChannelProvider tradeChannelProvider=tradeChannelProviderClass.newInstance();
        tradeChannelProvider.init(this);
      }
      catch (Exception e)
      {
        logger.error("========支付渠道实例化失败==========");
      }
    }
  }
}